<?php


namespace app\test\admin;


use app\admin\controller\Admin;
use app\admin\model\Config;
use app\common\builder\ZBuilder;
use app\test\model\Formula as FormulaModel;
use app\test\model\Formula;

class Index extends Admin
{
    /**
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function index()
    {
        // 查询
        $map = $this->getMap();
        // 数据列表
        $data_list = FormulaModel::where($map)->order('id desc')->paginate();

        return ZBuilder::make("table")
            ->addColumns([ // 批量添加数据列
                ['id', 'ID'],
                ['table_name', '数据库表名'],
                ['formula', '计算公式格式'],
                ['field_str', '字段参数'],
                ['remark', '备注'],
                ['right_button', '操作', 'btn'],
            ])
            ->addTopButton("add", '', 'true')
            ->addTopButton("delete")
            ->addRightButton("edit")
            ->addRightButton("delete")
            ->setRowList($data_list) // 设置表格数据
            ->fetch();
    }

    /**
     * @return mixed
     * @throws \think\Exception
     */
    public function add()
    {
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();

            // 验证
            $result = $this->validate($data, 'formula');
            if(true !== $result) $this->error($result);

            if (FormulaModel::create($data)) {
                $this->success('新增成功', url('index'));
            } else {
                $this->error('新增失败');
            }
        }

        return ZBuilder::make("form")
            ->addText("table_name", "数据库表名", "例如：table_name1", "", "", "oninput = \"value=value.replace(/[^\dA-Za-z_]/g,'')\"", "")
            ->addText("formula", "计算公式格式", "(占位符+占位符)/(占位符+占位符+占位符)，占位符为0到∞的正整数，例：(0+1)/(2+3+4)", "", "", "oninput = \"value=value.replace(/[^\d\+\-\*\/\(\)\{\}\[\]]/g,'')\"", "")
            ->addText("field_str", "字段参数", "field1,field2,field3", "", "", "oninput = \"value=value.replace(/[^\dA-Za-z,_]/g,'')\"")
            ->addText("remark", "备注", "", "", "", "", "")
//            ->setExtraJs($js)
            ->fetch();
    }

    /**
     * @param string $id
     * @return mixed
     * @throws \think\Exception
     */
    public function edit($id = '')
    {
        if ($id === null) $this->error('参数错误');

        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'formula');
            if(true !== $result) $this->error($result);

            if (FormulaModel::update($data)) {
                $this->success('修改成功', url('index'));
            } else {
                $this->error('修改失败');
            }
        }
        $info = FormulaModel::get($id);
        return ZBuilder::make("form")
            ->addHidden("id")
            ->addText("table_name", "数据库表名", "例如：table_name1", "", "", "oninput = \"value=value.replace(/[^\dA-Za-z_]/g,'')\"", "")
            ->addText("formula", "计算公式格式", "(占位符+占位符)/(占位符+占位符+占位符)，占位符为0到∞的正整数，例：(0+1)/(2+3+4)", "", "", "oninput = \"value=value.replace(/[^\d\+\-\*\/\(\)\{\}\[\]]/g,'')\"", "")
            ->addText("field_str", "字段参数", "field1,field2,field3", "", "", "oninput = \"value=value.replace(/[^\dA-Za-z,_]/g,'')\"")
            ->addText("remark", "备注", "", "", "", "", "")
            ->setFormData($info)
            ->fetch();
    }

    /**
     * @param array $record
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function delete($record = [])
    {
        $param = $this->request->param();
        $ids = $param['ids'];
        $where['id'] = $ids;
        if (is_array($ids)) {
            $result = FormulaModel::where('id', 'in', $ids)->update([
                'is_delete' => Config::get('constants.is_delete_yes'),
            ]);
        } else {
            $result = FormulaModel::where($where)->update([
                'is_delete' => Config::get('constants.is_delete_yes'),
            ]);
        }
        if ($result) {
            $this->success('删除成功', url('index'));
        } else {
            $this->error('删除失败');
        }
    }
}